<template>
  <div>
    <el-tabs class="Tabs" v-model="tabsValue">
      <el-tab-pane label="基础设置" name="1"></el-tab-pane>
      <el-tab-pane label="监听器" name="2"></el-tab-pane>
    </el-tabs>
    <el-form ref="form" class="startForm" :model="form" label-width="120px" size="small" :disabled="disabled">
      <template v-if="tabsValue === '1'">
        <slot name="form-item-task-name" :model="form" field="nodeCode">
          <el-form-item label="节点编码">
            <el-input v-model="form.nodeCode" :disabled="disabled"></el-input>
          </el-form-item>
        </slot>
        <slot name="form-item-task-name" :model="form" field="nodeName">
          <el-form-item label="节点名称">
            <el-input v-model="form.nodeName" :disabled="disabled"></el-input>
          </el-form-item>
        </slot>
      </template>
      <template v-if="tabsValue === '2'">
        <slot name="form-item-task-listenerType" :model="form" field="listenerType">
          <el-form-item prop="listenerRows" class="listenerItem" label-width="0">
            <el-table :data="form.listenerRows" style="width: 100%">
              <el-table-column prop="listenerType" label="类型" width="80">
                <template slot-scope="scope">
                  <el-form-item :prop="'listenerRows.' + scope.$index + '.listenerType'">
                    <el-select v-model="scope.row.listenerType" placeholder="请选择">
                      <el-option label="开始" value="start"></el-option>
                      <el-option label="分派" value="assignment"></el-option>
                      <el-option label="完成" value="finish"></el-option>
                      <el-option label="创建" value="create"></el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column prop="listenerPath" label="路径">
                <template slot-scope="scope">
                  <el-form-item :prop="'listenerRows.' + scope.$index + '.listenerPath'">
                    <el-input v-model="scope.row.listenerPath" placeholder="请输入"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="55" v-if="!disabled">
                <template slot-scope="scope">
                  <el-button type="danger" icon="el-icon-delete" @click="handleDeleteRow(scope.$index)"/>
                </template>
              </el-table-column>
            </el-table>
            <el-button v-if="!disabled" type="primary" style="margin-top: 10px;" @click="handleAddRow">增加行</el-button>
          </el-form-item>
        </slot>
      </template>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "Start",
  props: {
    value: {
      type: Object,
      default() {
        return {}
      }
    },
    disabled: { // 是否禁止
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      tabsValue: '1',
      form: this.value,
    }
  },
  watch: {
    form: {
      handler(n) {
        this.$emit('change', n)
      },
      deep: true
    }
  },
  created() {
    if (this.form.listenerType) {
      const listenerTypes = this.form.listenerType.split(",");
      const listenerPaths = this.form.listenerPath.split("@@");
      this.form.listenerRows = listenerTypes.map((type, index) => ({
        listenerType: type,
        listenerPath: listenerPaths[index]
      }));
    }
  },
  methods: {
    // 增加行
    handleAddRow() {
      this.form.listenerRows.push({listenerType: '', listenerPath: ''});
    },
    handleDeleteRow(index) {
      this.form.listenerRows.splice(index, 1);
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .Tabs {
  border: 0;
  margin: 0 20px;

  .el-tabs__content {
    display: none;
  }

  .el-tabs__item.is-active {
    margin-left: 0;
    border-top: 1px solid var(--el-border-color);
    margin-top: 0;
  }

}
.startForm {
  border-top: 0;
  padding: 15px;
}
</style>
